package javax.net.p2p.bio.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Socket;
import java.util.Scanner;
import javax.net.p2p.bio.handler.ChatClientHandler;
import javax.net.p2p.bio.server.ChatServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/****************************************************
 * @description 
 * @author   karl
 * @version  1.0, 2018-11-3
 * @see HISTORY
 *      Date        Desc          Author      Operation
 *  	2018-11-3   创建文件       karl        create
 * @since 2017 Phyrose Science & Technology (Kunming) Co., Ltd.
 **************************************************/
public class ChatClient {
    private static final Log log = LogFactory.getLog(ChatClient.class);

    public static void main(String[] args) throws Exception {

        try (
                Socket socket = new Socket("127.0.0.1", ChatServer.SERVER_PORT)) {
            //初始化客户端
            ChatClientHandler client = new ChatClientHandler(socket);
            //启动客户端线程读等待（服务器消息
            new Thread(client).start();
            System.out.println("客户端线程启动：" + socket);
            //处理控制台聊天消息
            System.out.println("请输入昵称：");
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
            client.processChat("login:" + name);
            while (true) {
                String msg = sc.nextLine();
                if ("logout".equalsIgnoreCase(msg)) {
                    break;
                }
                client.processChat(msg);
            }
        }
    }
}
